<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['init', 'getOrgInfo', 'index', 'getRecommendDesignList'];
    protected $noNeedRight = ['*'];

    public function init()
    {
        $data = [
            "needAuth" => (isset($_SERVER['RUNTIME_ENV']) && $_SERVER['RUNTIME_ENV'] && $_SERVER['RUNTIME_ENV'] == 'prod') ? '1' : '0',
        ];
        $this->success('', array_merge($data, self::getConfig()));
    }

    public function getOrgInfo()
    {
        $shopName = '';
        $shopLogo = '';
        $shopDesc = '';
        $shopList = model('\app\common\model\Config')->where('group', 'shop_info')->select();
        if ($shopList) {
            foreach ($shopList as $key => $shop) {
                if ($shop['name'] == 'shop_name') {
                    $shopName = $shop['value'];
                }
                if ($shop['name'] == 'shop_logo') {
                    $shopLogo = cdnurl($shop['value']);
                }
                if ($shop['name'] == 'shop_desc') {
                    $shopDesc = $shop['value'];
                }
            }
        }
        $data = [
            'shopName' => $shopName,
            'shopLogo' => $shopLogo,
            'shopDesc' => $shopDesc,
        ];
        $this->success('', $data);
    }

    /**
     * 首页
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->success('', self::getConfig());
    }

    public function getConfig()
    {
        $indexBannerList = [];
        $indexName = '';
        $indexLogo = '';
        $indexInfo = '';
        $shopList = model('\app\common\model\Config')->where('group', 'home_setting')->select();
        if ($shopList) {
            foreach ($shopList as $key => $shop) {
                if ($shop['name'] == 'index_name') {
                    $indexName = $shop['value'];
                }
                if ($shop['name'] == 'index_logo') {
                    $indexLogo = cdnurl($shop['value']);
                }
                if ($shop['name'] == 'index_info') {
                    $indexInfo = $shop['value'];
                }
                if ($shop['name'] == 'index_banner') {
                    $indexBannerVal = $shop['value'];
                    $indexBannerList = explode(',', $indexBannerVal);
                    foreach ($indexBannerList as $key2 => $banner) {
                        $indexBannerList[$key2] = cdnurl($indexBannerList[$key2]);
                    }
                }
            }
        }
        return [
            'indexBanner' => $indexBannerList,
            'indexName'   => $indexName,
            'indexLogo'   => $indexLogo,
            'indexInfo'   => $indexInfo,
        ];
    }

    public function getRecommendDesignList()
    {
        $this->success('', self::getDesignList());
    }

    private function getDesignList()
    {
        // 查询推荐的案例
        $designList = model('\app\admin\model\jy\Design')
            ->where(['status' => 2, 'is_del' => 0])
            ->order('weigh desc')
            ->select();
        // 查询案例的产品
        if ($designList) {
            foreach ($designList as &$design) {
                // 是否点赞
                if ($this->auth->id) {
                    $praise = model('\app\admin\model\jy\DesignPraise')->where(['user_id' => $this->auth->id, 'design_id' => $design['id']])->find();
                    if ($praise) {
                        $design['isPraise'] = 1;
                    } else {
                        $design['isPraise'] = 0;
                    }
                }
                $designProList = model('\app\admin\model\jy\DesignProduct')->where(['design_id' => $design['id'], 'is_del' => 0])->select();
                $dProList = [];
                if ($designProList) {
                    foreach ($designProList as $item) {
                        $prod = model('\app\admin\model\jy\Product')->where(['id' => $item['product_id'], 'is_del' => 0])->find();
                        if ($prod) {
                            $dProList[] = $prod['product_name'];
                        }
                    }
                }
                $design['productList'] = $dProList;
                $design['cover_images'] = cdnurl($design['cover_images']);
            }
        }
        return $designList;
    }
}
